﻿Imports System
Imports System.Data
Imports System.Configuration
Imports System.Collections
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports Microsoft.Reporting.WebForms
Imports log4net

Partial Class Report_RP03
    Inherits System.Web.UI.Page

    Dim Report As Object
    Public ScriptText As String
    Private Shared logger As ILog = LogManager.GetLogger("Report_RP03")

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init

        'If Session("fundcenterS") = "" Then
        '    Session("fundcenterS") = "0011"
        '    Session("fundcenterE") = "0012"

        'End If

        'If Session("users") = "" Then
        '    Session("users") = "wassana"
        'End If

        'Session("year") = 2556
        'Session("version") = 2


        If Session("users") Is Nothing Then
            Response.Redirect("../LoginForm.aspx")
        End If


        Getdata()


    End Sub


    Function Getdata() As Decimal


        Dim adapter As New Data_ReportTableAdapters.R03TableAdapter
        Dim sql As String
        Dim dt As New Data_Report.R03DataTable



        'sql = " select  pathname , (Users.FirstName + '  ' + Users.LastName)  as UserName "
        'sql += "  from fcth, fctr_view, users "
        'sql += "  where fcth.FundCenter_FundCenterCode = fctr_view.FundCenterCode"
        'sql += "  and fctr_view .UserName =Users.UserName "

        'sql += "  and users.FundCenter_PlanYear = " & Session("year")
        ''sql += " and fcth.planyear = " & Session("year")
        ''sql += " and fctr_view.PlanYear = " & Session("year")
        'Dim adapter2 As New fctrDSTableAdapters.fcth_viewTableAdapter
        'Dim dt2 As New fctrDS.fcth_viewDataTable

        'dt2 = adapter2.GetData(Session("users"), Val(Session("year")))
        'Dim prefil As String = "'0'"
        'For Each item As fctrDS.fcth_viewRow In dt2
        '    prefil += ",'" & item.FundCenterCode & "'"
        'Next
        'sql += " and fctr_view.FundCenterCode in (" & prefil & ") "
        'sql += " and fcth.FundCenter_FundCenterCode in (" & prefil & ") "
        'sql += "  order by   LEFT(grouppath,1),LEFT(grouppath,2),LEFT(grouppath,3) ,LEFT(grouppath,4),LEFT(grouppath,5)"


        sql = " select pathname , (ResponsibleAnalyst.FirstName + ' ' + ResponsibleAnalyst.LastName) as UserName "
        sql += "  from fcth, FundCenters, ResponsibleAnalyst "
        sql += "  where fcth.FundCenter_FundCenterCode = FundCenters.FundCenterCode "
        sql += " and fcth.PlanYear = " & Session("year")
        sql += " and FundCenters.ResponsibleAnalystCode = ResponsibleAnalyst.ResponsibleAnalystCode "
        sql += "  and FundCenters.PlanYear = " & Session("year")

        Dim adapter2 As New fctrDSTableAdapters.fcth_viewTableAdapter
        Dim dt2 As New fctrDS.fcth_viewDataTable

        dt2 = adapter2.GetData(Session("users"), Val(Session("year")))
        Dim prefil As String = "'0'"
        For Each item As fctrDS.fcth_viewRow In dt2
            prefil += ",'" & item.FundCenterCode & "'"
        Next
        sql += " and fcth.FundCenter_FundCenterCode in (" & prefil & ") "
        sql += "  order by   LEFT(grouppath,1),LEFT(grouppath,2),LEFT(grouppath,3) ,LEFT(grouppath,4),LEFT(grouppath,5)"

        adapter.SearchReport(dt, sql)

        'Response.Write(sql)
        dt.TableName = "R03DT"
        Dim das As New DataSet
        das.DataSetName = "R01DS"
        das.Tables.Add(dt)
        ReportViewer1.ProcessingMode = Microsoft.Reporting.WebForms.ProcessingMode.Local

        ReportViewer1.LocalReport.DataSources.Clear()
        ReportViewer1.LocalReport.DataSources.Add(New Microsoft.Reporting.WebForms.ReportDataSource("DataSet1", das.Tables("R03DT")))
        ReportViewer1.DocumentMapCollapsed = True

        Dim UserName As String = Session("users")
        Dim years As String = Session("year")
        Dim users As String = Session("users")

        Dim p1, p2 As ReportParameter
        p1 = New ReportParameter("year", years)
        p2 = New ReportParameter("username", UserName)

        ReportViewer1.LocalReport.SetParameters(p1)
        ReportViewer1.LocalReport.SetParameters(p2)


        ReportViewer1.LocalReport.Refresh()


        Session("count") = dt.Rows.Count



        Return dt.Rows.Count

    End Function


    Public Function nPageNumber() As String
        Dim str As String
        str = Me.Report.Globals!PageNumber.ToString()
        Return str
    End Function

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Session("users") Is Nothing Then
            ScriptText = "<script> wclose();</script>"
        End If
    End Sub
End Class
